<?php

namespace App\Http\Controllers;

use App\Models\Staffs;
use App\Util\Qrcode;
use Illuminate\Http\Request;

class StaffController extends Controller
{
    /**
     * 员工列表
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response
     */
    public function index()
    {
        $Staff = Staffs::join('members','staffs.member_id','=','members.id')
            ->where('boss',auth('member')->id())
            ->orderBy('id','desc')
            ->select('staffs.*','members.avatar','members.nickname')
            ->get();
        return response(["data"=>$Staff]);
    }

    /**
     * 通过员工审核
     * @param $_id
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response
     */
    public function pass($_id)
    {
        if(Staffs::where('id',$_id)->update(['status'=>1])) {
            $_response = response(['data'=>'success']);
        } else {
            $_response = response(['message'=>'操作失败'], 402);
        }
        return $_response;
    }

    /**
     * 删除员工
     * @param $_id
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response
     */
    public function destroy($_id)
    {
        if(Staffs::destroy($_id)) {
            $_response = response(['data'=>'success']);
        } else {
            $_response = response(['message'=>'删除失败'], 402);
        }
        return $_response;
    }

    /**
     * 申请员工二维码链接
     * @param Request $request
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response
     */
    public function applyUrl(Request $request)
    {
        $Qrcode = new Qrcode();
        $_data = array_merge(
            $request->data,
            array('boss'=>auth('member')->id())
        );
        $_params = $Qrcode->createParams($_data);
        return response(['data'=>route('scan',$_params)]);
    }

    /**
     * 测试申请员工二维码链接是否过期
     * @param Request $request
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response
     */
    public function testApplyUrl(Request $request)
    {
        $Qrcode = new Qrcode();
        $_data = array_merge(
            $request->data,
            array('boss'=>auth('member')->id())
        );
        $_result = array("data"=>1);
        if(!$Qrcode->testQrcode($_data)) {
            $_result = array("data"=>0);
        }
        return response($_result);
    }
}
